﻿using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace MyAppAPI
{
    public class JwtSettings
    {
        public string Issuer { get; }
        public string Audience { get; }
        public byte[] Key { get; }
        public JwtSettings(byte[] key,string issuer,string audience)
        {
            Key= key;
            Issuer= issuer;
            Audience= audience;
        }
        public TokenValidationParameters TokenValidationParameters => new TokenValidationParameters()
        {
            ValidateIssuer= true,
            ValidateAudience= true,
            ValidateIssuerSigningKey= true,
            ValidateLifetime= true,
            ValidIssuer= Issuer,
            ValidAudience=Audience,
            IssuerSigningKey=new SymmetricSecurityKey(Key),
        };
        public static JwtSettings FromConfiguration(IConfiguration configuration)
        {
            var issuser = configuration["Authentication:JwtBearer:Issuer"] ?? "default_issuer";
            var audiencer = configuration["Authentication:JwtBearer:Audience"] ?? "default_audience";
            var securityKey = configuration["Authentication:JwtBearer:SecurityKey"] ?? "default_securitykey";
            byte[] key = Encoding.ASCII.GetBytes(securityKey);
            return new JwtSettings(key, issuser, audiencer);
        }
    }
}
